{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run MMVT and open the sample subject with the DKT cortical atlas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'src'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-1f55827d8f95>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0msrc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmmvt_addon\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mscripts\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mrun_mmvt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0mmmvt\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrun_mmvt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'hc034'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'dkt'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'src'"
     ]
    }
   ],
   "source": [
    "from src.mmvt_addon.scripts import run_mmvt\n",
    "mmvt = run_mmvt.run('hc034', 'dkt')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load MEG source estimate from mne-python sample dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os.path as op\n",
    "import mne\n",
    "\n",
    "mne_sample_data_fol = mne.datasets.sample.data_path()\n",
    "meg_subject = 'sample'\n",
    "stc_fname = op.join(mne_sample_data_fol, 'MEG', 'sample', 'sample_audvis-meg-rh.stc')\n",
    "stc = mne.read_source_estimate(stc_fname)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create the Jupyter notebook widgets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import ipywidgets as widgets\n",
    "from ipywidgets import HBox, VBox, Label, Button, Layout, Image\n",
    "\n",
    "T = stc.lh_data.shape[1] \n",
    "play = widgets.Play(value=0, min=0, max=T, step=1, interval=1000)\n",
    "time_slider = widgets.IntSlider(value=0, min=0, max=T, step=1)\n",
    "widgets.jslink((play, 'value'), (time_slider, 'value'))\n",
    "quality_slider = widgets.IntSlider(value=40, min=0, max=100, step=1, layout=Layout(width='100%'))\n",
    "zoom_slider = widgets.IntSlider(value=0, min=-5, max=5, step=1, layout=Layout(width='100%'))\n",
    "threshold_slider = widgets.FloatSlider(value=2, min=0, max=10, step=0.1, layout=Layout(width='20%'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Smooth the source estimate and plot it in MMVT for t=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/autofs/space/thibault_001/users/npeled/anaconda3.4.4/lib/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "surface source space inferred...\n",
      "Computing morph matrix...\n",
      "    Left-hemisphere map read.\n",
      "    Right-hemisphere map read.\n",
      "    Left-hemisphere map read.\n",
      "    Right-hemisphere map read.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/autofs/space/thibault_001/users/npeled/anaconda3.4.4/lib/python3.6/site-packages/mne/utils.py:877: DeprecationWarning: Function morph_data is deprecated; This function is deprecated and will be removed in version 0.18. Use morph = mne.compute_source_morph(...) and morph(stc)\n",
      "  warnings.warn(msg, category=DeprecationWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    17 smooth iterations done.\n",
      "    14 smooth iterations done.\n",
      "[done]\n",
      "[done]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "def create_stc_t(stc, t):\n",
    "    data = np.concatenate([stc.lh_data[:, t:t + 1], stc.rh_data[:, t:t + 1]])\n",
    "    vertices = [stc.lh_vertno, stc.rh_vertno]\n",
    "    stc_t = mne.SourceEstimate(data, vertices, stc.tmin + t * stc.tstep, stc.tstep, subject=subject)\n",
    "    return stc_t\n",
    "\n",
    "def plot_stc(stc, t):\n",
    "    stc_t = create_stc_t(stc, t)\n",
    "    stc_t_smooth = mne.morph_data(meg_subject, subject, stc_t, n_jobs=n_jobs, grade=vertices_to)\n",
    "    meg_fname = mmvt.plot_stc_t(\n",
    "        stc_t_smooth.rh_data, stc_t_smooth.lh_data, t, threshold=threshold_slider.value, save_image=True)\n",
    "    mmvt.set_current_t(t)\n",
    "    return meg_fname, stc_t_smooth\n",
    "    \n",
    "n_jobs = 4\n",
    "t = 0 # ms\n",
    "vertices_to = mne.grade_to_vertices(subject, None)    \n",
    "meg_fname, stc_t_smooth = plot_stc(stc, t);\n",
    "file = open(meg_fname, \"rb\")\n",
    "meg_image = Image(value=file.read(), format='png', width=800, height=450)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create the buttons widgets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def update_image(fname=''):\n",
    "    fname = mmvt.save_image('stc', index=time_slider.value) if fname == '' else fname\n",
    "    file = open(fname, \"rb\")\n",
    "    meg_image.value = file.read()\n",
    "\n",
    "\n",
    "sagital_button = Button(description=\"Sagital\")\n",
    "sagital_button.on_click(lambda x:(mmvt.show_sagital(), mmvt.zoom(-1), update_image()))\n",
    "coronal_button = Button(description=\"Coronal\")\n",
    "coronal_button.on_click(lambda x:(mmvt.show_coronal(), mmvt.zoom(-1), update_image()))\n",
    "axial_button = Button(description=\"Axial\")\n",
    "axial_button.on_click(lambda x:(mmvt.show_axial(), mmvt.zoom(-1), update_image()))\n",
    "zoom_button = Button(description=\"zoom\")\n",
    "zoom_button.on_click(lambda x:(mmvt.zoom(zoom_slider.value), update_image()))\n",
    "render_button = Button(description='Render')\n",
    "update_button = Button(description='Update')\n",
    "update_button.on_click(lambda x:(update_image()))\n",
    "render_button.on_click(lambda x:(\n",
    "    mmvt.hide_subcorticals(), mmvt.set_brain_transparency(1), mmvt.set_to_camera_view(),\n",
    "    update_image(mmvt.render_image(quality=quality_slider.value))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Widget Javascript not detected.  It may not be installed or enabled properly.\n"
     ]
    },
    {
     "data": {},
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "surface source space inferred...\n",
      "Computing morph matrix...\n",
      "    Left-hemisphere map read.\n",
      "    Right-hemisphere map read.\n",
      "    Left-hemisphere map read.\n",
      "    Right-hemisphere map read.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/autofs/space/thibault_001/users/npeled/anaconda3.4.4/lib/python3.6/site-packages/mne/utils.py:877: DeprecationWarning: Function morph_data is deprecated; This function is deprecated and will be removed in version 0.18. Use morph = mne.compute_source_morph(...) and morph(stc)\n",
      "  warnings.warn(msg, category=DeprecationWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    17 smooth iterations done.\n",
      "    14 smooth iterations done.\n",
      "[done]\n",
      "[done]\n",
      "surface source space inferred...\n",
      "Computing morph matrix...\n",
      "    Left-hemisphere map read.\n",
      "    Right-hemisphere map read.\n",
      "    Left-hemisphere map read.\n",
      "    Right-hemisphere map read.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/autofs/space/thibault_001/users/npeled/anaconda3.4.4/lib/python3.6/site-packages/mne/utils.py:877: DeprecationWarning: Function morph_data is deprecated; This function is deprecated and will be removed in version 0.18. Use morph = mne.compute_source_morph(...) and morph(stc)\n",
      "  warnings.warn(msg, category=DeprecationWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    17 smooth iterations done.\n",
      "    14 smooth iterations done.\n",
      "[done]\n",
      "[done]\n",
      "surface source space inferred...\n",
      "Computing morph matrix...\n",
      "    Left-hemisphere map read.\n",
      "    Right-hemisphere map read.\n",
      "    Left-hemisphere map read.\n",
      "    Right-hemisphere map read.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/autofs/space/thibault_001/users/npeled/anaconda3.4.4/lib/python3.6/site-packages/mne/utils.py:877: DeprecationWarning: Function morph_data is deprecated; This function is deprecated and will be removed in version 0.18. Use morph = mne.compute_source_morph(...) and morph(stc)\n",
      "  warnings.warn(msg, category=DeprecationWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    17 smooth iterations done.\n",
      "    14 smooth iterations done.\n",
      "[done]\n",
      "[done]\n",
      "surface source space inferred...\n",
      "Computing morph matrix...\n",
      "    Left-hemisphere map read.\n",
      "    Right-hemisphere map read.\n",
      "    Left-hemisphere map read.\n",
      "    Right-hemisphere map read.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/autofs/space/thibault_001/users/npeled/anaconda3.4.4/lib/python3.6/site-packages/mne/utils.py:877: DeprecationWarning: Function morph_data is deprecated; This function is deprecated and will be removed in version 0.18. Use morph = mne.compute_source_morph(...) and morph(stc)\n",
      "  warnings.warn(msg, category=DeprecationWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    17 smooth iterations done.\n",
      "    14 smooth iterations done.\n",
      "[done]\n",
      "[done]\n"
     ]
    }
   ],
   "source": [
    "def on_value_change(change):\n",
    "    t = change['new']\n",
    "    meg_fname, stc_t_smooth = plot_stc(stc, t)\n",
    "    file = open(meg_fname, \"rb\")\n",
    "    meg_image.value = file.read()\n",
    "\n",
    "time_slider.observe(on_value_change, names='value') \n",
    "VBox([HBox([meg_image, VBox(\n",
    "    [sagital_button, coronal_button, axial_button, update_button,\n",
    "    Label('Quality:'), quality_slider, render_button])]),\n",
    "      HBox([play, time_slider, Label('Threshold:'), threshold_slider])])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "widgets": {
   "state": {
    "6380c829e07a4e9da693513cd2f0011b": {
     "views": [
      {
       "cell_index": 7
      }
     ]
    }
   },
   "version": "1.2.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
